package cn.ds.assets.module.assets.controller.admin.metadataDatasource;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ds.assets.framework.common.pojo.PageResult;
import cn.ds.assets.framework.common.pojo.CommonResult;
import static cn.ds.assets.framework.common.pojo.CommonResult.success;

import cn.ds.assets.framework.excel.core.util.ExcelUtils;

import cn.ds.assets.framework.operatelog.core.annotations.OperateLog;
import static cn.ds.assets.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ds.assets.module.assets.controller.admin.metadataDatasource.vo.*;
import cn.ds.assets.module.assets.dal.dataobject.metadataDatasource.BaseMetadataDatasourceDO;
import cn.ds.assets.module.assets.convert.metadataDatasource.BaseMetadataDatasourceConvert;
import cn.ds.assets.module.assets.service.metadataDatasource.BaseMetadataDatasourceService;

//@Tag(name = "管理后台 - 元数据管理-数据源")
//@RestController
//@RequestMapping("/assets/base-metadata-datasource")
@Validated
public class BaseMetadataDatasourceController {

    @Resource
    private BaseMetadataDatasourceService baseMetadataDatasourceService;

    @PostMapping("/create")
    @Operation(summary = "创建元数据管理-数据源")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:create')")
    public CommonResult<Long> createBaseMetadataDatasource(@Valid @RequestBody BaseMetadataDatasourceCreateReqVO createReqVO) {
        return success(baseMetadataDatasourceService.createBaseMetadataDatasource(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新元数据管理-数据源")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:update')")
    public CommonResult<Boolean> updateBaseMetadataDatasource(@Valid @RequestBody BaseMetadataDatasourceUpdateReqVO updateReqVO) {
        baseMetadataDatasourceService.updateBaseMetadataDatasource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除元数据管理-数据源")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:delete')")
    public CommonResult<Boolean> deleteBaseMetadataDatasource(@RequestParam("id") Long id) {
        baseMetadataDatasourceService.deleteBaseMetadataDatasource(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得元数据管理-数据源")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:query')")
    public CommonResult<BaseMetadataDatasourceRespVO> getBaseMetadataDatasource(@RequestParam("id") Long id) {
        BaseMetadataDatasourceDO baseMetadataDatasource = baseMetadataDatasourceService.getBaseMetadataDatasource(id);
        return success(BaseMetadataDatasourceConvert.INSTANCE.convert(baseMetadataDatasource));
    }

    @GetMapping("/list")
    @Operation(summary = "获得元数据管理-数据源列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:query')")
    public CommonResult<List<BaseMetadataDatasourceRespVO>> getBaseMetadataDatasourceList(@RequestParam("ids") Collection<Long> ids) {
        List<BaseMetadataDatasourceDO> list = baseMetadataDatasourceService.getBaseMetadataDatasourceList(ids);
        return success(BaseMetadataDatasourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得元数据管理-数据源分页")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:query')")
    public CommonResult<PageResult<BaseMetadataDatasourceRespVO>> getBaseMetadataDatasourcePage(@Valid BaseMetadataDatasourcePageReqVO pageVO) {
        PageResult<BaseMetadataDatasourceDO> pageResult = baseMetadataDatasourceService.getBaseMetadataDatasourcePage(pageVO);
        return success(BaseMetadataDatasourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出元数据管理-数据源 Excel")
    @PreAuthorize("@ss.hasPermission('assets:base-metadata-datasource:export')")
    @OperateLog(type = EXPORT)
    public void exportBaseMetadataDatasourceExcel(@Valid BaseMetadataDatasourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BaseMetadataDatasourceDO> list = baseMetadataDatasourceService.getBaseMetadataDatasourceList(exportReqVO);
        // 导出 Excel
        List<BaseMetadataDatasourceExcelVO> datas = BaseMetadataDatasourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "元数据管理-数据源.xls", "数据", BaseMetadataDatasourceExcelVO.class, datas);
    }

}
